<?php

namespace App\Http\Controllers\Admin;

use App\Config;
use App\Http\Response;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Log;

class UserController extends Controller
{
    use Response;
    //获取用户列表
    public function index(Request $request)
    {
        $qtype=$request->get('qtype');
        $query_keywords=$request->get('query');
        $paginate=User::with(['parent','store'])->when($qtype,function ($query)use ($query_keywords,$qtype){
            switch ($qtype){
                case "id":
                    $query->where('id','=',$query_keywords);
                    break;
                case "nickname":
                    $query->where('nickname','like','%'.$query_keywords.'%');
                    break;
            }
        })->latest()->paginate($request->get('pageSize'),'*','pageNo');
        return $this->success(200,$paginate);
    }
    //获取用户信息
    public function show($id)
    {
        $user=User::find($id);
      return $this->success(200,$user);
    }
    public function update($id,Request $request)
    {
       $data=$request->all();
       $update_info=User::where('id',$id)->update($data);
       if ($update_info){
           return $this->message(200,'修改成功');
       }else{
           return $this->error(505,'修改失败');
       }
    }
    public function store()
    {
        $data=[
            'name'=>'test',
            'email'=>'test@qq.com',
            'password'=>Hash::make('test123456'),
            'username'=>'test',
            'phoneNumber'=>1123123,
            'sponsor_id'=>0,
            'first_name'=>'111',
            'last_name'=>'222',
            'shipping_address'=>"1231321",
            'id_card_number'=>'1121132'
        ];
        User::create($data);
    }
    public function destroy($id)
    {
      User::destroy($id);
        return $this->message(200, '删除成功');
    }

//    public function setConfig(Request $request)
//    {
//        $where=[
//            ['mch_id','=',auth('merchant')->id()],
//            ['title','=','register']
//        ];
//        $find=Config::where($where)->first();
//        if (!$find){
//            $data=[
//                'title'=>'register',
//                'value'=>$request->all(),
//                'mch_id'=>auth('merchant')->id()
//            ];
//            $created=Config::create($data);
//            if ($created){
//                return $this->message(200,trans('topic.create success'));
//            }else{
//                return $this->message(505,trans('topic.create failed'));
//            }
//        }else{
//            return $this->message(505,trans('topic.create failed'));
//        }
//    }
//    public function getConfig()
//    {
//        $where=[
//            ['mch_id','=',auth('merchant')->id()],
//            ['title','=','register']
//        ];
//        $find=Config::where($where)->first();
//        return $this->success(200,$find);
//    }
//    public function updateConfig(Request $request)
//    {
//        $where=[
//            ['mch_id','=',auth('merchant')->id()],
//            ['title','=','register']
//        ];
//        $update=Config::where($where)->update(['value'=>json_encode($request->all())]);
//        if ($update){
//            return $this->message(200,trans('topic.update success'));
//        }else{
//            return $this->error(505,trans('topic.update failed'));
//        }
//    }
}
